package com.example.courier.mapper;

import com.example.courier.entity.DeliveryOrder;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author qwert
 * @version 1.0.0
 * @create 2025/9/6 10:41
 **/
@Mapper
public interface DeliveryOrderMapper {

    @Insert("""
INSERT INTO ha_deliver_order (delivery_order_id, requester_id, courier_id, pickup_address, pickup_details,
 delivery_address, pickup_address, goods_type,status, fee, create_time, scheduled_time, note, pickup_code,
 delivery_proof, is_paid, coupon_id)
VALUES (#{deliveryOrderId}, #{requesterId}, #{courierId}, #{pickupAddress}, #{pickupDetails},
 #{deliveryAddress}, #{deliveryDetails}, #{goodsType}, #{status}, #{fee}, now(), #{scheduledTime},
 #{note}, #{pickupCode}, #{deliveryProof}, #{isPaid}, #{couponId});
""")
    void saveDeliveryOrder(DeliveryOrder deliveryOrder);

    @Select("""
<script>
    select * from ha_deliver_order where 1 = 1
    <if test="requester_id != null and requester_id != ''">
        AND requester_id = #{requesterId}
    </if>
</script>
""")
    List<DeliveryOrder> queryDeliveryOrderByRequesterId(String requesterId);

    @Select("""
<script>
    select * from ha_deliver_order 
    where 1 = 1
    <if test="requesterId != null and requesterId != ''">
        AND requester_id = #{requesterId}
    </if>
    <if test="pickupAddress != null and pickupAddress != ''">
        AND pickup_address = #{pickupAddress}
    </if>
    <if test="deliveryAddress != null and deliveryAddress != ''">
        AND delivery_address = #{deliveryAddress}
    </if>
    <if test="status != null and status != ''">
        AND status = #{status}
    </if>
</script>
""")
    List<DeliveryOrder> queryDeliveryOrderByPrams(@Param("requesterId") String requesterId,
                                                  @Param("pickupAddress") String pickupAddress,
                                                  @Param("deliveryAddress") String deliveryAddress,
                                                  @Param("status") String status);

    @Select("""
<script>
    select * 
    from ha_deliver_order 
    where delivery_order_id in
    <foreach collection='orderIds' item='orderId' open="(" separator="," close=")">
      #{orderId}
    </foreach>
    and courier_id is null
</script>
""")
    List<DeliveryOrder> queryDeliveryOrderStatus(List<String> orderIds);

    @Update("""
<script>
    update ha_deliver_order 
    set courier_id = 
        <foreach collection='deliveryOrders' item='order'>
                #{order.courierId}
        </foreach>
        , status = 
        <foreach collection='deliveryOrders' item='order'>
                #{order.status}
        </foreach>
    where delivery_order_id in
         <foreach collection='deliveryOrders' item='order' open="(" separator="," close=")">
            #{order.deliveryOrderId}
        </foreach>
</script>
""")
    void acceptOrders(List<DeliveryOrder> deliveryOrders);

    @Select("""
<script>
    select * from ha_deliver_order 
    where delivery_order_id in
        <foreach collection='orderIds' item='orderId' open="(" separator="," close=")">
          #{orderId}
        </foreach>
        and courier_id = #{courierId}
        and status = #{status}
</script>
""")
    List<DeliveryOrder> queryDeliveryOrderProgressing(@Param("courierId") String courierId,
                                                      @Param("orderIds") List<String> orderIds,
                                                      @Param("status") String status);

    @Update("""
<script>
    update ha_deliver_order 
    set status = #{status}
    where delivery_order_id in
         <foreach collection='orderIds' item='orderId' open="(" separator="," close=")">
            #{orderId}
        </foreach>
</script>
""")
    void completeOrders(@Param("orderIds") List<String> orderIds,
                        @Param("status") String status);
}
